/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package expression.interval;

/**
 *
 * @author ddoose
 */
public class IntRightBounded extends Interval {
    private int bound;
    
    IntRightBounded(int b) {
        bound = b;
    }
    
    public boolean isEmpty() {
        return false;
    }
    
    public boolean isUniverse() {
        return false;
    }
    
    public boolean isValue() {
        return false;
    }

    public int getValue() {
        throw new UnsupportedOperationException("IntLeftBounded has no value");
    }

    public boolean isLeftBounded() {
        return false;
    }
    
    public int getLeftBound() {
        throw new UnsupportedOperationException("IntRightBounded has no right bound");
    }
    
    public boolean isRightBounded() {
        return true;
    }
    
    public int getRightBound() {
        return bound;
    }

    public Interval inter(Interval i) {
        if (i.isStrictRight(this)) {
            return new IntEmpty();
        }
        else if (i.isRightBounded()) {
            int r = Math.min(bound, i.getRightBound());

            if (i.isLeftBounded()) {
                if (i.getLeftBound() == r) {
                    return new IntValue(r);
                }
                else {
                    return new IntBounded(i.getLeftBound(), r);
                }
            }
            else {
                return new IntRightBounded(r);
            }
        } 
        else if (i.isLeftBounded()) {
            if (bound == i.getLeftBound()) {
                return new IntValue(bound);
            } 
            else {
                return new IntBounded(i.getLeftBound(), bound);
            }
        }
        else {
            return clone();
        }
    }
    
    public Interval union(Interval i) {
        if (i.isUniverse()) {
            return i.clone();
        }
        if (i.isEmpty()) {
            return null;
        }
        else if (i.isLeftBounded() && (i.getLeftBound() <= bound +1)) {
            if (i.isRightBounded()) {
                return new IntRightBounded(Math.max(i.getRightBound(), bound));
            }
            else {
                return new IntUniverse();
            }
        }
        else if (i.isRightBounded() && (i.getRightBound() <= bound +1)) {
            return new IntRightBounded(Math.min(bound, i.getRightBound()));
        }
        return null;
    }
    
    public boolean equals(Interval i) {
        return (i instanceof IntRightBounded) && (bound == i.getRightBound());
    }

    public Interval clone() {
        return new IntRightBounded(bound);
    }

    public String toString() {
        return "]- ; " + bound + "]";
    }
}
